
#include "Sqlite3DB.h"

#include "db/sqlite3/sqlite3.24.0/sqlite3.h"


static const std::string STRNAME = "sqlite3";
static const std::string STRVERSION = SQLITE_VERSION;

const std::string& Sqlite3DB::name()
{
    return STRNAME;
}

const std::string& Sqlite3DB::version()
{
    return STRVERSION;
}

int Sqlite3DB::version_id()
{
    return SQLITE_VERSION_NUMBER;
}

Sqlite3DB::~Sqlite3DB()
{
    close();
}

bool Sqlite3DB::_open()
{
    close();
    int ret = sqlite3_open_v2(config_.SQLiteFile.c_str(), &handle_, config_.SQLiteFlags, nullptr);
    if (SQLITE_OK == ret && config_.SQLiteBusyTime > 0){
        ret = sqlite3_busy_timeout(handle_, config_.SQLiteBusyTime);
    }
    if (SQLITE_OK != ret) {
        return false;
    }
    return true;
}

void Sqlite3DB::close()
{
    if (handle_ != nullptr) {
        sqlite3_close(handle_);
        handle_ = nullptr;
    }
}

std::string Sqlite3DB::last_error()
{
    return sqlite3_errmsg(handle_);
}

uint64_t Sqlite3DB::last_insert_id()
{
    return sqlite3_last_insert_rowid(handle_);
}



